package com.springboot.jdbctemplate.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.jdbctemplate.entity.Menu;
import com.springboot.jdbctemplate.service.IMenuService;

@RestController
@RequestMapping("/menu")
public class MenuController {
	private IMenuService menuService;

	@Autowired
	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public List<Menu> getAccounts() {
		return menuService.findAccountList();
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Menu getAccountById(@PathVariable("id") String id) {
		return menuService.findAccountById(id);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public String updateAccount(@PathVariable("id") String id,
			@RequestParam(value = "name", required = true) String name) {
		Menu menu = new Menu();
		menu.setName(name);
		menu.setId(id);
		int t = menuService.update(menu);
		if (t == 1) {
			return menu.toString();
		} else {
			return "fail";
		}
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public String postAccount(@RequestParam(value = "id") String id) {
		Menu menu = new Menu();
		menu.setId(id);
		int t = menuService.add(menu);
		if (t == 1) {
			return menu.toString();
		} else {
			return "fail";
		}

	}

}
